package com.houduan.model;

import java.util.Date;

public class Session {
    private int id; // 上机记录编号
    private User user; // 用户
    private Computer computer; // 电脑
    private Date startTime; // 开始时间
    private Date endTime; // 结束时间
    private double duration; // 时长（小时）
    private double cost; // 费用

    // 无参构造方法
    public Session() {
    }

    // 全参构造方法
    public Session(int id, User user, Computer computer, Date startTime, Date endTime, double duration, double cost) {
        this.id = id;
        this.user = user;
        this.computer = computer;
        this.startTime = startTime;
        this.endTime = endTime;
        this.duration = duration;
        this.cost = cost;
    }

    // Getter 和 Setter 方法

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Computer getComputer() {
        return computer;
    }

    public void setComputer(Computer computer) {
        this.computer = computer;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public double getDuration() {
        return duration;
    }

    public void setDuration(double duration) {
        this.duration = duration;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    // 重写 toString 方法
    @Override
    public String toString() {
        return "Session{" +
                "id=" + id +
                ", user=" + user +
                ", computer=" + computer +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", duration=" + duration +
                ", cost=" + cost +
                '}';
    }
    public double calculateDuration() {
        if (startTime == null) {
            return 0;
        }
        Date now = (endTime == null) ? new Date() : endTime;
        return (now.getTime() - startTime.getTime()) / (1000.0 * 60 * 60); // 转换为小时
    }

    public double calculateCost() {
        if (computer == null) {
            return 0;
        }
        return calculateDuration() * computer.getPrice(); // 时长 * 每小时价格
    }
}
